//
//  BCPutCoinContentScrollView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/29.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCPutCoinContentScrollView.h"

//
#import "BCTitleTextFieldView.h"
#import "BCPutCoinNumbersView.h"

@interface BCPutCoinContentCashNumberView ()

@property (nonatomic, strong) UILabel *captionLabel;

@property (nonatomic, strong) UILabel *numberLabel;

/**
 *caption
 */
@property (nonatomic, copy) NSString *caption;

/**
 *number
 */
@property (nonatomic, copy) NSString *number;

@end

@implementation BCPutCoinContentCashNumberView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.captionLabel = [UILabel new];
        self.captionLabel.textColor = [UIColor qmui_colorWithHexString:@"#666666"];
        self.captionLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        self.captionLabel.numberOfLines = 2;
        [self addSubview:self.captionLabel];
        [self.captionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self).offset(20.0);
            make.centerY.mas_equalTo(self);
            make.right.mas_equalTo(self.mas_centerX).offset(-5.0);
        }];
        //
        self.numberLabel = [UILabel new];
        self.numberLabel.textColor = [UIColor qmui_colorWithHexString:@"#666666"];
        self.numberLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        self.numberLabel.adjustsFontSizeToFitWidth = true;
        self.numberLabel.minimumScaleFactor = .8;
        self.numberLabel.textAlignment = NSTextAlignmentRight;
        [self addSubview:self.numberLabel];
        [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self).offset(-20.0);
            make.centerY.mas_equalTo(self);
            make.left.mas_equalTo(self.mas_centerX);
        }];
        //
        self.numberLabel.text = nil;
    }
    return self;
}

- (void)setCaption:(NSString *)caption {
    _caption = caption;
    _captionLabel.text = caption;
}
- (void)setNumber:(NSString *)number {
    _number = number;
    //
    if (number.length) {
        self.numberLabel.text = number;
    }else {
        self.numberLabel.text = @"--";
    }
}

+ (CGFloat)defaultHeight {
    return 56.0;
}

@end

@interface BCPutCoinContentScrollView () <BCPutCoinNumbersViewDelegate>

@property (nonatomic, strong) UIView *contentView;

/**
 *BCTitleTextFieldView
 */
@property (nonatomic, strong) BCTitleTextFieldView *putAddressView;

/**
 *BCTitleTextFieldView
 */
@property (nonatomic, strong) BCPutCoinNumbersView *putNumbersView;
/**
 *available
 */
@property (nonatomic, strong) UILabel *availableLabel;


/**
 *BCTitleTextFieldView
 */
@property (nonatomic, strong) BCTitleTextFieldView *putFeeView;

/**
 *captionLabel
 */
@property (nonatomic, strong) QMUILabel *captionLabel;

@end

@implementation BCPutCoinContentScrollView

- (BCPutCoinContentCashNumberView *)cashNumberView {
    if (!_cashNumberView) {
        _cashNumberView = [BCPutCoinContentCashNumberView new];
    }
    return _cashNumberView;
}


- (instancetype)init
{
    self = [super init];
    if (self) {
        self.showsVerticalScrollIndicator = false;
        self.alwaysBounceVertical = true;
        [self setupSubviews];
        //
        self.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    }
    return self;
}
- (void)setupSubviews {
    self.contentView = [UIView new];
    [self addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(self);
        make.width.mas_equalTo(self);
    }];
    [self setupContentViews];
}
//
- (void)setupContentViews {
    CGFloat defaultHeight = [BCTitleTextFieldView defaultHeight];
    self.putAddressView = [BCTitleTextFieldView new];
    [self.contentView addSubview:self.putAddressView];
    [self.putAddressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.contentView);
        make.top.mas_equalTo(self.contentView);
        make.height.mas_equalTo(defaultHeight);
    }];
    //添加二维码按钮
    self.qrCodeButton = [QMUIButton new];
    [self.qrCodeButton setImage:LOAD_IMAGE(@"wallet_cash_qr_code") forState:UIControlStateNormal];
    [self.putAddressView addSubview:self.qrCodeButton];
    self.qrCodeButton.contentEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    [self.qrCodeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.putAddressView);
        make.right.mas_lessThanOrEqualTo(self.putAddressView);
    }];
    
    
    //
    self.putNumbersView = [BCPutCoinNumbersView new];
    self.putNumbersView.delegate = self;
    [self.contentView addSubview:self.putNumbersView];
    [self.putNumbersView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.contentView);
        make.top.mas_equalTo(self.putAddressView.mas_bottom).offset(15.0);
        make.height.mas_equalTo(defaultHeight);
    }];
    //
    self.availableLabel = [UILabel new];
    self.availableLabel.numberOfLines = 0;
    self.availableLabel.textColor = [UIColor qmui_colorWithHexString:@"#666666"];
    self.availableLabel.font = [UIFont systemFontOfSize:11.0 weight:UIFontWeightRegular];
    [self.contentView addSubview:self.availableLabel];
    [self.availableLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.putNumbersView.mas_bottom).offset(5.0);
        make.left.mas_equalTo(self.contentView).offset(20.0);
        make.right.mas_equalTo(self.contentView).offset(-20.0);
    }];
    //
    self.putFeeView = [BCTitleTextFieldView new];
    self.putFeeView.textField.enabled = false;
    [self.contentView addSubview:self.putFeeView];
    [self.putFeeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.contentView);
        make.top.mas_equalTo(self.availableLabel.mas_bottom).offset(15.0);
        make.height.mas_equalTo(defaultHeight);
    }];
    //
    self.captionLabel = [QMUILabel new];
    self.captionLabel.textColor = [UIColor qmui_colorWithHexString:@"#AEAEAE"];
    self.captionLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
    self.captionLabel.numberOfLines = 0;
    [self.contentView addSubview:self.captionLabel];
    [self.captionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.putFeeView.mas_bottom).offset(20.0);
        make.left.mas_equalTo(self.contentView).offset(20.0);
        make.right.mas_equalTo(self.contentView).offset(-20.0);
        make.bottom.mas_equalTo(self.contentView);
    }];
}

- (void)setContent:(BCUserWalletCashData *)content {
    _content = content;
    //
    if (content) {
        self.putNumbersView.digits = MAX(content.data.scaler.length-1, 0);
        //
        self.putAddressView.title = content.flow.addr;
        self.putAddressView.placeholder = content.flow.addr_placeholder;
        //
        self.putNumbersView.title = content.flow.amount;
        NSString *amount_placeholderReplacing = [content.data.min_cash stringByAppendingString:@""];
        self.putNumbersView.placeholder = [content.flow.amount_placeholder stringByReplacingOccurrencesOfString:@"{}" withString:amount_placeholderReplacing];
        //
        NSString *available = [content.flow.available stringByAppendingString:[content.data.balance stringByAppendingString:content.data.acronyms]];
        self.availableLabel.text = available;
//        [self.availableLabel sizeToFit];
        //
        self.putFeeView.title = content.flow.handling;
        self.putFeeView.placeholder = content.flow.handling;
        //
        if (content.data.handling_type == BCUserWalletCashDataHandlingType_1) {//固定金额
            self.putFeeView.textField.text = content.data.handling;
        }else if (content.data.handling_type == BCUserWalletCashDataHandlingType_2) {//百分比
            CGFloat handling = content.data.handling.floatValue;
            CGFloat handling100 = handling*100;
            self.putFeeView.textField.text = [NSString stringWithFormat:@"%.2f%%",handling100];
        }else {//固定比例
            self.putFeeView.textField.text = content.data.handling;
        }
        //
        NSString *promptReplacing = [content.data.min_cash stringByAppendingString:content.data.acronyms];
        self.captionLabel.text = [content.flow.prompt stringByReplacingOccurrencesOfString:@"{}" withString:promptReplacing];
        //
        [self.captionLabel sizeToFit];
        [self layoutIfNeeded];
        
        //
        CGSize contentSize = [self.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
        self.contentSize = CGSizeMake(0, contentSize.height+20);
        
        //
        self.cashNumberView.caption = content.flow.arrival;
        //
        if (!self.cashNumberView.number) {
            self.cashNumberView.number = @"--";
        }
    }
}

#pragma mark - BCPutCoinNumbersViewDelegate
- (void)putCoinNumbersViewTextDidChanged:(NSString *)text {
    if (text.length) {
        //余额
        CGFloat balance = self.content.data.balance.floatValue;
        CGFloat inputNumber = text.floatValue;
        if (inputNumber>balance) {
            self.cashNumberView.number = nil;
        }else {
            CGFloat number = 0;
            //扣除的
            CGFloat handling = self.content.data.handling.floatValue;
            if (self.content.data.handling_type==BCUserWalletCashDataHandlingType_2) {//百分比
                number = inputNumber*(1-handling);
            }else {//固定扣除
                number = inputNumber-handling;
            }
            //确认小数点位数
            NSInteger digits = MAX(self.content.data.scaler.length-1, 0);
            //
            self.cashNumberView.number = [self number:number digits:digits];
        }
    }else {
        self.cashNumberView.number = nil;
    }
}
- (NSString *)number:(CGFloat)number digits:(NSInteger)digits {
    switch (digits) {
        case 0:
            return [NSString stringWithFormat:@"%.0f",number];
            break;
        case 1:
            return [NSString stringWithFormat:@"%.1f",number];
            break;
        case 2:
            return [NSString stringWithFormat:@"%.2f",number];
            break;
        case 3:
            return [NSString stringWithFormat:@"%.3f",number];
            break;
        case 4:
            return [NSString stringWithFormat:@"%.4f",number];
            break;
        case 5:
            return [NSString stringWithFormat:@"%.5f",number];
            break;
        case 6:
            return [NSString stringWithFormat:@"%.6f",number];
            break;
        case 7:
            return [NSString stringWithFormat:@"%.7f",number];
            break;
        case 8:
            return [NSString stringWithFormat:@"%.8f",number];
            break;
        case 9:
            return [NSString stringWithFormat:@"%.9f",number];
            break;
        default:
            return [NSString stringWithFormat:@"%.10f",number];
            break;
    }
}




- (void)clearInputData {
    self.putAddressView.textField.text = nil;
    self.putNumbersView.textField.text = nil;
}


- (void)showContent:(BOOL)show {
    self.contentView.alpha = show?1:0;
    self.cashNumberView.alpha = self.contentView.alpha;
    [self clearInputData];
}

/**
 设置提币地址
 */
- (void)setCoinAddress:(NSString *)address {
    self.putAddressView.textField.text = address;
}



- (NSString *)address {
    NSString *text = self.putAddressView.textField.text;
    if (text.length==0) {
        [self.putAddressView.textField becomeFirstResponder];
    }
    return text;
}
- (NSString *)number {
    NSString *text = self.putNumbersView.textField.text;
    if (text.length==0) {
        [self.putNumbersView.textField becomeFirstResponder];
    }
    return text;
}

@end
